package com.niuwa.p2p.action.producttemplate;

import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.DictConstant;
import com.niuwa.p2p.entity.productTemplate.AreaPropCfg;
import com.niuwa.p2p.entity.productTemplate.ProductProtocol;
import com.niuwa.p2p.entity.productTemplate.ProductTemplate;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.productTemplate.IInvestProdTemplateService;
import com.niuwa.p2p.service.productTemplate.IProjectProdTemplateService;
import com.niuwa.p2p.service.productTemplate.ProductService;
import com.niuwa.p2p.service.protocol.IProtocolService;
import com.niuwa.p2p.vo.productTemplate.InvestProdTemplateVo;
import com.niuwa.p2p.vo.productTemplate.ProjectProdTmptForm;
import com.niuwa.p2p.vo.productTemplate.ProjectProdTmptVo;
import com.niuwa.p2p.vo.protocol.ProductProtocolVo;
import com.opensymphony.xwork2.ActionContext;

public class ProjectProductTmpltOperAction extends JsonAction {

	private static final long serialVersionUID = -164220153058789615L;
	
	@Autowired
	private IProtocolService protocolService;
	
	@Autowired
	private IInvestProdTemplateService investProdTemplateService;
	
	@Autowired
	private IProjectProdTemplateService iBorrowProdTemplateService;
	
	@Autowired
	private ProductService productService;
	
	private List<ProductProtocolVo> protocols;
	
	private ProjectProdTmptForm tmptForm;
	
	private List<AreaPropCfg> apcList;

	private List<ProductProtocol> ppList;
	
	/** 产品模板ID */
	private long tempId;
	
	/** 拒绝理由 */
	private String rejectReason;
	
	/**
	 * 跳转到添加借款模板视图
	 * 
	 * @return 借款模板视图
	 * 
	 */
	@Action("gotoAddProjectProdTemplate")
	public String gotoAddProjectProdTemplate() {
		// 获取所有协议信息
		protocols = protocolService.selectAllProtocol();

		return "addProjectProdTemplate";
	}
	
	/**
	 * 添加借款模板
	 * 
	 * @return NONE
	 */
	@Action("addProjectProdTemplate")
	public String addProjectProdTemplate() {
		JsonResult result = new JsonResult();
		// 获取用户ID
		long userId = SessionUtil.getSessionUser().getId();
		try {
			ProductTemplate productTemplate = new ProductTemplate();
			productTemplate.setTempName(tmptForm.getTempName());
			productTemplate.setMainVersion(Integer.parseInt(tmptForm.getMainVersion()));
			productTemplate.setMinorVersion(Integer.parseInt(tmptForm.getMinorVersion()));
			/*判断是否存在模版名称、版本号比输入的版本号高的模版*/
			int findProductTmptByName = investProdTemplateService.findProductTmptByName(productTemplate);
			if (findProductTmptByName > 0) {
				result.setSuccess(false);
				result.setMsg("版本号不允许重复，新版本不得低于旧版本");
			}else {
				iBorrowProdTemplateService.addProjectProductTmplt(userId, tmptForm);
				result.setSuccess(true);
				result.setMsg("操作成功");
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg("操作失败");
		}
		this.setResult(result);
		return NONE;
	}
	
	/**
 	 * 跳转到借款模板列表页面
 	 * @return
 	 */
 	@Action("gotoProjectProdTemplate")
	public String gotoProjectProdTemplate() {
		return "projectProductTemplate";
	}
 	
	/**
	 * 借款模板列表页面分页查询
	 * @return
	 */
 	@Action("projectListProductTemplt")
 	public String projectListProductTemplt() {
		// 获取Grid条件选择参数
		Map<String, Object> paramMap = getParamMap();
		//产品模板类型：借款
		paramMap.put("tempType", DictConstant.TempType.PROJECT);
		// 执行查询操作
		PageBean<InvestProdTemplateVo> pagination = investProdTemplateService.search(paramMap);
		// 设置返回
		setObjectReSult(pagination);
		return NONE;
 	}
 	
 	/**
	 * 查看借款项目模板
	 * 
	 * @return showProjectProdTemplate.jsp
	 */
	@Action("showProjectProdTemplate")
	public String showProjectProdTemplate() {
		ActionContext context = ActionContext.getContext();
		
		ProjectProdTmptVo prod = iBorrowProdTemplateService.getProdTmptById(tempId);
		
		tmptForm = prod.getTmptForm();
		tmptForm.setMinorVersion(String.valueOf(Long.valueOf(tmptForm.getMinorVersion()) - 1));
		context.put("tmptForm", tmptForm);
		
		apcList = prod.getApcList();
		ActionContext.getContext().put("apcList", apcList);
		
		ppList = prod.getPpList();
		ActionContext.getContext().put("ppList", ppList);

		return "showProjectProdTemplate";
	}
	
	/**
	 * 修改产品模板
	 * 
	 * @return
	 */
	@Action("updateProjectProdTemplate")
	public String updateProjectProdTemplate(){
		
		ActionContext context = ActionContext.getContext();
		
		ProjectProdTmptVo prod = iBorrowProdTemplateService.getProdTmptById(tempId);
		
		tmptForm = prod.getTmptForm();
		tmptForm.setMinorVersion(String.valueOf(Long.valueOf(tmptForm.getMinorVersion()) - 1));
		context.put("tmptForm", tmptForm);
		
		apcList = prod.getApcList();
		ActionContext.getContext().put("apcList", apcList);
		
		ppList = prod.getPpList();
		ActionContext.getContext().put("ppList", ppList);
		
		// 获取所有协议信息
		protocols = protocolService.selectAllProtocol();
		
		return "updateProjectProdTemplate";
	}

	/**
	 * 删除产品模板 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(失败,产品模板不存在)
	 * @throws Exception 
	 */
	@Action("deleteProjectProductTmplt")
	public String deleteProjectProductTmplt() throws Exception {
		JsonResult result = new JsonResult();		
		int num = investProdTemplateService.deleteProductTmplt(tempId);	
		if(num > 0){
			result.setSuccess(true);
			result.setMsg("操作成功");
		}else{
			result.setSuccess(false);
			result.setMsg("该模板不能删除");
		}
		this.setResult(result);
		return NONE;
	}

 	/**
	 * 产品模板审批通过 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(失败,产品模板不存在)
	 * @throws Exception 
	 */
	@Action("passProjectTmplt")
	public String passProjectTmplt() throws Exception {
		JsonResult result = new JsonResult();		
		investProdTemplateService.updateProductTmpltToPass(tempId, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	
 	/**
	 * 产品模板审批拒绝 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(失败,产品模板不存在)
	 * @throws Exception 
	 */
	@Action("rejectProjectTmplt")
	public String rejectProjectTmplt() throws Exception {
		JsonResult result = new JsonResult();		
		investProdTemplateService.updateProductTmpltToReject(tempId, rejectReason, SessionUtil.getSessionUser().getId());		
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
	
	@Action("getNewAudit")
	public String getNewAudit(){
		Integer tempId1 = Integer.parseInt(String.valueOf(tempId)); 
		this.setResult(true, null, productService.getNewstAudit(tempId1));
		return NONE;
	}
	
	
	public List<ProductProtocolVo> getProtocols() {
		return protocols;
	}

	public void setProtocols(List<ProductProtocolVo> protocols) {
		this.protocols = protocols;
	}

	public ProjectProdTmptForm getTmptForm() {
		return tmptForm;
	}

	public void setTmptForm(ProjectProdTmptForm tmptForm) {
		this.tmptForm = tmptForm;
	}

	public long getTempId() {
		return tempId;
	}

	public void setTempId(long tempId) {
		this.tempId = tempId;
	}

	public List<AreaPropCfg> getApcList() {
		return apcList;
	}

	public void setApcList(List<AreaPropCfg> apcList) {
		this.apcList = apcList;
	}

	public List<ProductProtocol> getPpList() {
		return ppList;
	}

	public void setPpList(List<ProductProtocol> ppList) {
		this.ppList = ppList;
	}

	public String getRejectReason() {
		return rejectReason;
	}

	public void setRejectReason(String rejectReason) {
		this.rejectReason = rejectReason;
	}

}
